<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>两数之和 - 算法详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            overflow-x: auto;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .string { color: #98c379; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; font-style: italic; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .badge-hover {
            transition: all 0.2s ease;
        }
        .badge-hover:hover {
            transform: scale(1.05);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="flex items-center mb-4">
                <i class="fas fa-code text-4xl mr-4 opacity-80"></i>
                <span class="text-lg font-light tracking-wider">算法精讲</span>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">两数之和</h1>
            <p class="text-xl md:text-2xl font-light leading-relaxed max-w-3xl opacity-90">
                经典算法题目深度解析，掌握哈希表与双指针技巧
            </p>
            <div class="mt-8 flex flex-wrap gap-4">
                <span class="badge-hover inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                    <i class="fas fa-hashtag mr-2"></i>哈希表
                </span>
                <span class="badge-hover inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                    <i class="fas fa-arrows-alt-h mr-2"></i>双指针
                </span>
                <span class="badge-hover inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>O(n) 时间复杂度
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- 题目描述卡片 -->
        <div class="card-hover bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <div class="w-12 h-12 bg-gradient-to-br from-purple-500 to-pink-500 rounded-xl flex items-center justify-center mr-4">
                    <i class="fas fa-question text-white text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed">
                    <span class="drop-cap">给</span>定一个整数数组和一个目标值，找出数组中两个数字，使它们相加等于目标值。假设每个输入都有唯一解，同一个元素不能使用两次。
                </p>
                <div class="mt-6 p-6 bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl border-l-4 border-purple-500">
                    <p class="font-semibold text-purple-800 mb-2">
                        <i class="fas fa-lightbulb mr-2"></i>示例
                    </p>
                    <p class="text-gray-700">
                        给定数组 <code class="px-2 py-1 bg-gray-800 text-purple-300 rounded">[2, 7, 11, 15]</code> 和目标值 <code class="px-2 py-1 bg-gray-800 text-purple-300 rounded">9</code>
                    </p>
                    <p class="text-gray-700 mt-2">
                        因为 <code class="px-2 py-1 bg-gray-800 text-purple-300 rounded">2 + 7 = 9</code>，所以返回 <code class="px-2 py-1 bg-gray-800 text-purple-300 rounded">[0, 1]</code>（这两个数字的索引）
                    </p>
                </div>
            </div>
        </div>

        <!-- 算法可视化 -->
        <div class="card-hover bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <div class="w-12 h-12 bg-gradient-to-br from-blue-500 to-cyan-500 rounded-xl flex items-center justify-center mr-4">
                    <i class="fas fa-project-diagram text-white text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">算法流程图</h2>
            </div>
            <div class="mermaid">
                graph TD
                    A[开始] --> B[创建空哈希表]
                    B --> C[遍历数组]
                    C --> D{target - num 在哈希表中?}
                    D -->|是| E[返回索引对]
                    D -->|否| F[将 num 和索引存入哈希表]
                    F --> G{遍历结束?}
                    G -->|否| C
                    G -->|是| H[返回空数组]
                    E --> I[结束]
                    H --> I
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- 解题思路 -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <!-- 哈希表方法 -->
            <div class="card-hover bg-white rounded-2xl shadow-xl p-8">
                <div class="flex items-center mb-6">
                    <div class="w-10 h-10 bg-gradient-to-br from-green-500 to-teal-500 rounded-lg flex items-center justify-center mr-3">
                        <i class="fas fa-hashtag text-white"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">哈希表方法</h3>
                </div>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">遍历数组，对于每个元素 <code class="px-2 py-1 bg-gray-100 text-purple-600 rounded text-sm">nums[i]</code></p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">检查 <code class="px-2 py-1 bg-gray-100 text-purple-600 rounded text-sm">target - nums[i]</code> 是否在哈希表中</p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">若存在，返回对应的索引对；否则存入哈希表</p>
                    </div>
                    <div class="mt-6 p-4 bg-gradient-to-r from-green-50 to-teal-50 rounded-lg">
                        <p class="text-sm font-semibold text-gray-700 mb-2">复杂度分析</p>
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-clock mr-2 text-green-600"></i>时间复杂度：O(n)
                        </p>
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-memory mr-2 text-blue-600"></i>空间复杂度：O(n)
                        </p>
                    </div>
                </div>
            </div>

            <!-- 双指针方法 -->
            <div class="card-hover bg-white rounded-2xl shadow-xl p-8">
                <div class="flex items-center mb-6">
                    <div class="w-10 h-10 bg-gradient-to-br from-orange-500 to-red-500 rounded-lg flex items-center justify-center mr-3">
                        <i class="fas fa-arrows-alt-h text-white"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">双指针方法</h3>
                </div>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-orange-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">先对数组进行排序</p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-orange-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">使用左右两个指针从两端向中间移动</p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-orange-500 mt-1 mr-3"></i>
                        <p class="text-gray-700">根据当前和与目标值的大小关系移动指针</p>
                    </div>
                    <div class="mt-6 p-4 bg-gradient-to-r from-orange-50 to-red-50 rounded-lg">
                        <p class="text-sm font-semibold text-gray-700 mb-2">复杂度分析</p>
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-clock mr-2 text-orange-600"></i>时间复杂度：O(n log n)
                        </p>
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-memory mr-2 text-blue-600"></i>空间复杂度：O(1)
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 代码实现 -->
        <div class="card-hover bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <div class="w-12 h-12 bg-gradient-to-br from-indigo-500 to-purple-500 rounded-xl flex items-center justify-center mr-4">
                    <i class="fas fa-code text-white text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">示例代码</h2>
            </div>
            <div class="mb-4">
                <span class="inline-flex items-center px-3 py-1 bg-indigo-100 text-indigo-800 rounded-full text-sm font-medium">
                    <i class="fab fa-python mr-2"></i>Python 实现
                </span>
            </div>
            <div class="code-block p-6 font-mono text-sm">
                <pre><code><span class="keyword">def</span> <span class="function">twoSum</span>(nums, target):
    <span class="comment"># 创建哈希表存储已遍历的数字和索引</span>
    hash_map = {}
    
    <span class="keyword">for</span> i, num <span class="keyword">in</span> <span class="function">enumerate</span>(nums):
        <span class="comment"># 计算需要的配对数字</span>
        complement = target - num
        
        <span class="comment"># 检查配对数字是否已存在</span>
        <span class="keyword">if</span> complement <span class="keyword">in</span> hash_map:
            <span class="keyword">return</span> [hash_map[complement], i]
        
        <span class="comment"># 将当前数字和索引存入哈希表</span>
        hash_map[num